﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using ic2012Model;
using System.ServiceModel.Activation;

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "TaskService" in code, svc and config file together.
public class TaskService : ITaskService
{
    public TaskData GetTask(long id)
    {
        using (ic2012Entities entities = new ic2012Entities())
        {
            TaskData task = new TaskData();
            var retrievedTask = (Task)(from t in entities.Tasks
                                                       where t.ID == id
                                                       select t).First();

            task.Task = retrievedTask;

            return task;
        }
    }

    public List<TaskData> GetAllTasks()
    {
        List<TaskData> tasksList = new List<TaskData>();
        using (ic2012Entities entities = new ic2012Entities())
        {
            var tasks = from t in entities.Tasks select t;
            foreach (Task retrievedTask in tasks)
            {
                TaskData task = new TaskData();
                task.Task = retrievedTask;

                tasksList.Add(task);
            }
        }
        return tasksList;
    }

    public List<TaskData> GetTasksInRange(double latitude, double longitude, double radius)
    {
        List<TaskData> tasksList = new List<TaskData>();
        using (ic2012Entities entities = new ic2012Entities())
        {
            var tasks = from t in entities.Tasks
                        where (t.Lat <= latitude + radius) && (t.Lat >= latitude - radius)
                            && (t.Long <= longitude + radius) && (t.Long >= longitude - radius)
                        select t;
            foreach (Task retrievedTask in tasks)
            {
                TaskData task = new TaskData();
                task.Task = retrievedTask;

                tasksList.Add(task);
            }
            return tasksList;
        }
    }
}
